package com.module.user.ui.completePersonalInfo

import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.common.app.base.AppActivity
import com.common.app.core.RouterHub
import com.common.app.data.bean.BaseCityModel
import com.common.app.data.bean.KeyEvents
import com.common.app.utls.Constants
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.extras.no
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.setOnClickListener
import com.common.base.app.extras.showToast
import com.common.base.app.extras.yes
import com.common.base.utils.StatusBarUtils
import com.common.base.utils.ViewUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.module.user.R
import com.module.user.databinding.UserActivityCompletePersonalInfoBinding

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：28/6/2025
 * <p>
 * 描述：完善个人信息页面
 * <p>
 * 修订历史：
 */
@Route(path = RouterHub.ROUTER_COM_PER_INFO_ACTIVITY)
class CompletePersonalInfoActivity : AppActivity<UserActivityCompletePersonalInfoBinding, CompletePersonalInfoViewModel>(),View.OnClickListener {

    override fun getViewModel() = CompletePersonalInfoViewModel::class.java

    override fun getViewContentBinding() = UserActivityCompletePersonalInfoBinding.inflate(layoutInflater)

    override fun isStatusBarTranslate(): Boolean = true
    override fun isShowToolBar(): Boolean = false

    private val dateBirthSelectDialog :DateBirthSelectDialog? by lazy{DateBirthSelectDialog(this)}
    private val genderSelectDialog :GenderSelectDialog? by lazy{GenderSelectDialog(this)}
    private val heightSelectDialog :HeightSelectDialog? by lazy{HeightSelectDialog(this)}
    private val weightSelectDialog :WeightSelectDialog? by lazy{WeightSelectDialog(this)}

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)

        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle("")
        headTitleUtils.setLineStyle(10)

        mViewContentBinding.kvGender.tag = 1
    }

    override fun lazyInitData() {
        super.lazyInitData()
        Constants.userBean?.let {
            mViewContentBinding.entNikeName.setText(it.nickname)
            it.birthday.isNullOrEmpty().no {
                mViewContentBinding.entDateBirth.setValue(it.birthday)
            }
            //0女1男
            mViewContentBinding.kvGender.setValue(when(it.sex){
                0-> getString(R.string.common_sex_female_mm_hint)
                else-> getString(R.string.common_sex_male_mm_hint)
            })

            mViewContentBinding.kvGender.tag = it.sex

            (it.height>0).yes {
                mViewContentBinding.kvHeight.setValue(it.height.toString())
            }
            (it.weight>0).yes {
                mViewContentBinding.kvBodyWeight.setValue(it.weight.toString())
            }
        }
    }


    override fun initEvents() {
        super.initEvents()

        dateBirthSelectDialog?.setListener(object : DateBirthSelectDialog.OnSelectListener{
            override fun onSelectClick(
                yearBean: BaseCityModel?,
                monthBean: BaseCityModel?,
                dayBean: BaseCityModel?
            ) {
                mViewContentBinding.entDateBirth.setValue("${yearBean?.code}-${monthBean?.code}-${dayBean?.code}")
            }
        })
        genderSelectDialog?.setListener(object : GenderSelectDialog.OnSelectListener{
            override fun onSelectClick(mBean: BaseCityModel) {
                mViewContentBinding.kvGender.setValue(mBean.name)
                mViewContentBinding.kvGender.tag = mBean.code.toInt()
            }
        })
        heightSelectDialog?.setListener(object : HeightSelectDialog.OnSelectListener{
            override fun onSelectClick(mBean: BaseCityModel) {
                mViewContentBinding.kvHeight.setValue(mBean.name)
                mViewContentBinding.kvHeight.tag = mBean.code.toInt()
            }
        })
        weightSelectDialog?.setListener(object : WeightSelectDialog.OnSelectListener{
            override fun onSelectClick(mBean: BaseCityModel) {
                mViewContentBinding.kvBodyWeight.setValue(mBean.name)
                mViewContentBinding.kvBodyWeight.tag = mBean.code.toInt()
            }
        })

        mViewContentBinding.entDateBirth.setOnClickListener(onClick = this)
        mViewContentBinding.kvGender.setOnClickListener(onClick = this)
        mViewContentBinding.kvHeight.setOnClickListener(onClick = this)
        mViewContentBinding.kvBodyWeight.setOnClickListener(onClick = this)

        mViewContentBinding.tvOK.setOnClickListener(onClick = this)

    }

    override fun onClick(v: View?) {
        when (v) {
            mViewContentBinding.entDateBirth -> {
                dateBirthSelectDialog?.show()
            }
            mViewContentBinding.kvGender -> {
                genderSelectDialog?.show()
            }
            mViewContentBinding.kvHeight -> {
                heightSelectDialog?.show()
            }
            mViewContentBinding.kvBodyWeight -> {
                weightSelectDialog?.show()
            }
            mViewContentBinding.tvOK -> {
                modifyUsrInfo()
            }
        }
    }

    override fun onBack(view: View?) {
//        super.onBack(view)
    }

    override fun onBackPressedSupport() {
//        super.onBackPressedSupport()
    }

    private fun modifyUsrInfo() {
        val mNikeName = mViewContentBinding.entNikeName.text.toString().trim()
        if (mNikeName.isNullOrEmpty()){
            showToast(R.string.user_com_per_info_ac_input_nile_name_hint)
            return
        }
        val mDateBirth = mViewContentBinding.entDateBirth.getValue().trim()
        if (mDateBirth.isNullOrEmpty()){
            showToast(R.string.user_com_per_info_ac_date_birth_hint)
            return
        }
        val mGender = mViewContentBinding.kvGender.getValue().trim()
        if (mGender.isNullOrEmpty()){
            showToast(R.string.user_com_per_info_ac_gender_hint)
            return
        }
        val mHeight = mViewContentBinding.kvHeight.getValue().trim()
        if (mHeight.isNullOrEmpty()){
            showToast(R.string.user_com_per_info_ac_height_hint)
            return
        }
        val mBodyWeight = mViewContentBinding.kvBodyWeight.getValue().trim()
        if (mBodyWeight.isNullOrEmpty()){
            showToast(R.string.user_com_per_info_ac_body_weight_hint)
            return
        }

        showDialogLoading()
        mViewModel.modifyUsrInfo(nickname = mNikeName, birthday = mDateBirth, sex = mViewContentBinding.kvGender.tag.toString().toInt(), height = mHeight.toInt(), weight = mBodyWeight.toInt()).observe(this) {
            dismissLoadingDialog()
            it.isSuccess().yes {
                LiveEventBus.get(KeyEvents.KEY_REQUEST_USER_INFO_DATA).post(this)
                finish()
            }.otherwise {
                showToast(it.Message)
            }
        }
    }

}